/*
 * Copyright (c) 2017. 深圳掌控网络有限公司. All rights reserved.
 */

package com.yiqi.lottery.feature.find.presenter;

import android.text.TextUtils;
import com.yiqi.lottery.entity.bo.LotteryHistoryBo;
import com.yiqi.lottery.entity.bo.LotteryResultBo;
import com.yiqi.lottery.feature.find.contract.ResultContract;
import com.yiqi.lottery.feature.find.dependencies.circle.DaggerCirclePresenterComponent;
import com.yiqi.lottery.feature.find.dependencies.result.DaggerResultPresenterComponent;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import javax.inject.Inject;
import java.util.List;

/**
 *      LIJUWEN
 * @email yiyayiyayaoljw@gmail.com
 * @date 2017/4/11 9:31
 */
public class ResultPresenter implements ResultContract.Presenter {

    private ResultContract.View mView;

    @Inject

    ResultContract.Model mModel;


    public ResultPresenter(ResultContract.View mView) {
        this.mView = mView;
          DaggerResultPresenterComponent.create().inject(this);
    }

    @Override
    public void start() {

    }


    @Override
    public void getLotteryResult(int listen) {
        mModel.getLotteryResult(listen).compose(mView.bindToLife()).subscribe(new Observer<List<LotteryResultBo>>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(List<LotteryResultBo> resultBos) {
                mView.dismissLoading();
                mView.showLotteryResultSuccess(resultBos);

            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showLotteryError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void getLotteryHistory(String label) {
        if (TextUtils.isEmpty(label)){
            return;
        }
        mModel.getLotteryHistory(label).compose(mView.bindToLife()).subscribe(new Observer<List<LotteryHistoryBo>>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(List<LotteryHistoryBo> resultBos) {
                mView.dismissLoading();
             if (resultBos.size()>0){
                 mView.showLotteryHistorySuccess(resultBos);
             }else {
                 mView.showLotteryEmpty();
             }

            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showLotteryError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }
}
